package com.oscarmovie.action;

import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Comment;
import com.oscarmovie.model.Image;
import com.oscarmovie.model.Paging;
import com.oscarmovie.model.Person;
import com.oscarmovie.service.CommentService;
import com.oscarmovie.service.PersonService;

public class PersonAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private PersonService personService;
	private CommentService commentService;
	private Person person;
	private List<Person> persons;
	private String imagesData;
	private List<Comment> comments;//不能直接用person.getComments()，要用CommentService来取
	
	private Paging paging = new Paging();
	public String add() throws Exception {
		setPerson(null);
		imagesData = "";
		return Constants.ADD;
	}
	public String edit() throws Exception {
		persons = personService.list(paging);
		setPerson(personService.load(person.getId()));
		imagesData = "";
		Set<Image> images = person.getImages();
		if(images!= null){
			for(Image image: images){
				imagesData += image.getId() + ",";
			}
		}
		return Constants.EDIT;
	}
	public String save() throws Exception {
		//edit - transfer data to existing object
		if(person.getId()!=0){
			Person existingPerson = personService.load(person.getId());
			existingPerson.setName(person.getName());
			existingPerson.setNameCn(person.getNameCn());
			existingPerson.setAlias(person.getAlias());
			existingPerson.setMtimeId(person.getMtimeId());
			existingPerson.setDoubanId(person.getDoubanId());
			
			if(person.getImage().getId()!=0){
				existingPerson.setImage(person.getImage());
			}
			existingPerson.setBiography(person.getBiography());
			person = existingPerson;
		}
		person.setName(person.getName().trim());
		person.setNameCn(person.getNameCn().trim());
		
		
		String imageIds[] = imagesData.split(",");
		if (!"".equals(imagesData)) {
			for (String id : imageIds) {
				Image image = new Image();
				image.setId(Integer.parseInt(id));
				person.getImages().add(image);
			}
		}

		if (person.getImage()==null || person.getImage().getId() == 0)
			person.setImage(null);
		personService.save(person);
		return Constants.SUCCESS;
	}

	public String load() throws Exception {
		this.setPerson(personService.load(person.getId()));
		paging.setPageSize(10);
		comments = commentService.listByPersonId(person.getId(), paging);
		return Constants.DETAIL;
	}
	public String image() throws Exception {
		this.setPerson(personService.load(person.getId()));
		return Constants.IMAGES;
	}
	public String list() throws Exception {
		
		if(person!=null && !"".equals(person.getName())){
			
			Person p = new Person();
			String n = person.getName().trim();
			p.setName(n);
			p.setNameCn(n);
			
			persons = personService.list(paging,p);
		} else {
			
			persons = personService.list(paging);
		}
		
		
		return Constants.LIST;
	}

	public String select() throws Exception {
		
		return Constants.SELECT;
	}
	
	public List<Person> getPersons() {
		return persons;
	}


	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Person getPerson() {
		return person;
	}
	public void setImagesData(String imagesData) {
		this.imagesData = imagesData;
	}
	public String getImagesData() {
		return imagesData;
	}
	public void setPaging(Paging paging) {
		this.paging = paging;
	}
	public Paging getPaging() {
		return paging;
	}
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}
	public List<Comment> getComments() {
		return comments;
	}
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	public CommentService getCommentService() {
		return commentService;
	}

}
